﻿using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Update.Internal;
using Microsoft.EntityFrameworkCore.Update;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace M.Migration
{
    //[SuppressMessage("Usage", "EF1001:Internal EF Core API usage.")]
    //public class MigrationsModelDifferWithoutForeignKey : MigrationsModelDiffer
    //{
    //    public MigrationsModelDifferWithoutForeignKey(
    //        IRelationalTypeMappingSource typeMappingSource,
    //        IMigrationsAnnotationProvider migrationsAnnotations,
    //        IChangeDetector changeDetector,
    //        IUpdateAdapterFactory updateAdapterFactory,
    //        CommandBatchPreparerDependencies commandBatchPreparerDependencies) :
    //        base(typeMappingSource,
    //            migrationsAnnotations,
    //            changeDetector,
    //            updateAdapterFactory,
    //            commandBatchPreparerDependencies)
    //    {
    //    }

    //    public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalModel? source, IRelationalModel? target)
    //    {
    //        var operations = base.GetDifferences(source, target)
    //            .Where(op => op is not AddForeignKeyOperation)
    //            .Where(op => op is not DropForeignKeyOperation)
    //            .ToList();

    //        foreach (var operation in operations.OfType<CreateTableOperation>())
    //            operation.ForeignKeys?.Clear();

    //        return operations;
    //    }
    //}

    /// <summary>
    /// efcore创建迁移时移除外键的创建，在options中配置
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "EF1001:Internal EF Core API usage.", Justification = "<挂起>")]
    public class MigrationsModelDifferWithoutForeignKey : MigrationsModelDiffer
    {
        public MigrationsModelDifferWithoutForeignKey
            ([NotNull] IRelationalTypeMappingSource typeMappingSource,
             [NotNull] IMigrationsAnnotationProvider migrationsAnnotations,
             IRowIdentityMapFactory rowIdentityMapFactory,
             CommandBatchPreparerDependencies commandBatchPreparerDependencies)
            : base(typeMappingSource, migrationsAnnotations, rowIdentityMapFactory, commandBatchPreparerDependencies)
        {
        }

        public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalModel? source, IRelationalModel? target)
        {
            var operations = base.GetDifferences(source, target)
                .Where(op => op is not AddForeignKeyOperation)
                .Where(op => op is not DropForeignKeyOperation)
                .ToList();

            foreach (var operation in operations.OfType<CreateTableOperation>())
                operation.ForeignKeys?.Clear();

            return operations;
        }
    }
}